package com.event.management.frontend.service;

import com.event.management.frontend.config.ArkProperties;
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionRequest;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessage;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole;
import com.volcengine.ark.runtime.service.ArkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ArkAiService {

    @Autowired
    private ArkService arkService;
    
    @Autowired
    private ArkProperties properties;

    /**
     * 处理用户消息并获取AI回复
     *
     * @param userMessage 用户消息内容
     * @return AI回答内容
     */
    public String processMessage(String userMessage) {
        List<ChatMessage> messages = new ArrayList<>();
        
        // 添加系统提示
        ChatMessage systemMessage = ChatMessage.builder()
                .role(ChatMessageRole.SYSTEM)
                .content(properties.getSystemPrompt())
                .build();
        
        // 添加用户提示
        ChatMessage userPrompt = ChatMessage.builder()
                .role(ChatMessageRole.USER)
                .content(userMessage)
                .build();
        
        messages.add(systemMessage);
        messages.add(userPrompt);
        
        ChatCompletionRequest request = ChatCompletionRequest.builder()
                .model(properties.getModel())
                .messages(messages)
                .build();

        StringBuilder result = new StringBuilder();
        arkService.createChatCompletion(request)
                .getChoices()
                .forEach(choice -> result.append(choice.getMessage().getContent()));
        
        return result.toString();
    }
} 